package com.qdlc.p2p.dal.model;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import com.qdlc.p2p.common.exception.BusinessException;
import com.qdlc.p2p.common.exception.TppChinapnrException;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.dto.TppMerchantLog;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.util.UserUtils;

/**
 * 第三方商户操作记录 Model
 * 
 * @author ylx
 * @version 2.0
 * @date 2015年6月18日 下午4:49:58
 */
public class TppMerchantLogModel extends TppMerchantLog {

	
	/**
	 * 未处理
	 */
	public final static byte STATUS_UNTREATED = 0;
	
	/**
	 * 处理成功
	 */
	public final static byte STATUS_SUCEESS = 1;
	
	/**
	 * 处理失败
	 */
	public final static byte STATUS_FAIL = 2;
	
	/**
	 * 商户充值
	 */
	public final static byte TYPE_MERCHANT_RECHARGE = 1;
	
	/**
	 * 商户取现
	 */
	public final static byte TYPE_MERCHANT_CASH = 2;
	
	/**
	 * 商户子账户互转
	 */
	public final static byte TYPE_MERCHANT_TRANSFER = 3;
	
	/**
	 * 商户给用户转账
	 */
	public final static byte TYPE_TRANSFER = 4;
	
	/**
	 * 页数
	 */
	private int page;
	/**
	 * 行数
	 */
	private int rows;
	
	/**
	 * 条件查询
	 */
	private String searchName;
	
	/**
	 * 开始日期
	 */
	private String startTime;
	
	/**
	 * 结束日期
	 */
	private String endTime;
	
	/**
	 * 操作员用户名
	 */
	private String opName;;
	
	/**
	 * 状态文字描述
	 */
	private String statusStr;
	
	/**
	 * 操作类型文字描述 
	 */
	private String typeStr;
	
	/** 锁定会话 **/
	private String rechargeToken;

	/** 是否需生成资金记录 **/
	private int isAccountLog;
	
	/**
	 * 实例化model
	 * 
	 * @param merchantLog
	 * @return
	 */
	public static TppMerchantLogModel instance(TppMerchantLog merchantLog) {
		TppMerchantLogModel tppMerchantLogModel = new TppMerchantLogModel();
		BeanUtils.copyProperties(merchantLog, tppMerchantLogModel);
		return tppMerchantLogModel;
	}
	
	/**
	 * 充值校验
	 */
	public void validMoney() {
		if (this.getMoney() < 0.01) {
			throw new TppChinapnrException("充值金额过小，请重新输入金额", BusinessException.TYPE_JSON);
		}
		if (this.getMoney() >= 10000000) {
			throw new TppChinapnrException("充值金额过大，目前仅支持千万级别充值", BusinessException.TYPE_JSON);
		}
	}
	
	/**
	 * 转账参数校验
	 */
	public void validTransfer() {
		if (StringUtil.isBlank(this.getAccount())) {
			throw new TppChinapnrException("付款方不能为空", BusinessException.TYPE_JSON);
		}
		if (StringUtil.isBlank(this.getToAccount())) {
			throw new TppChinapnrException("收款方不能为空", BusinessException.TYPE_JSON);
		}
		if (this.getMoney() < 0.01) {
			throw new TppChinapnrException("转账金额过小", BusinessException.TYPE_JSON);
		}
	}
	
	/**
	 * 校验收款账户
	 * 
	 * @param user
	 */
	public void validUser(User user) {
		if (user == null) {
			throw new TppChinapnrException("收款方不存在", BusinessException.TYPE_JSON);
		}
		String tppUserCustId = UserUtils.getUserTppCustId(user.getUserId());
		if (StringUtils.isBlank(tppUserCustId)) {
			throw new TppChinapnrException("收款方没有开户", BusinessException.TYPE_JSON);
		}
	}
	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public String getSearchName() {
		return searchName;
	}

	public void setSearchName(String searchName) {
		this.searchName = searchName;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getOpName() {
		return opName;
	}

	public void setOpName(String opName) {
		this.opName = opName;
	}

	public String getStatusStr() {
		switch (getStatus()) {
		case TppMerchantLogModel.STATUS_FAIL:
			this.statusStr = "失败";
			break;
		case TppMerchantLogModel.STATUS_SUCEESS:
			this.statusStr = "成功";
			break;
		case TppMerchantLogModel.STATUS_UNTREATED:
			this.statusStr = "未处理";
			break;
		default:
			break;
		}
		return statusStr;
	}

	public void setStatusStr(String statusStr) {
		this.statusStr = statusStr;
	}

	public String getTypeStr() {
		switch (getType()) {
		case TppMerchantLogModel.TYPE_MERCHANT_CASH:
			this.typeStr = "平台取现";
			break;
		case TppMerchantLogModel.TYPE_MERCHANT_RECHARGE:
			this.typeStr = "平台充值";
			break;
		case TppMerchantLogModel.TYPE_MERCHANT_TRANSFER:
			this.typeStr = "账户互转";
			break;
		case TppMerchantLogModel.TYPE_TRANSFER:
			this.typeStr = "账户转账";
			break;
		default:
			break;
		}
		return typeStr;
	}

	public void setTypeStr(String typeStr) {
		this.typeStr = typeStr;
	}

	public String getRechargeToken() {
		return rechargeToken;
	}

	public void setRechargeToken(String rechargeToken) {
		this.rechargeToken = rechargeToken;
	}

	public int getIsAccountLog() {
		return isAccountLog;
	}

	public void setIsAccountLog(int isAccountLog) {
		this.isAccountLog = isAccountLog;
	}
}
